package com.jqsoft.launcher.activity.gairui;


import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.jqsoft.launcher.activity.JQActivity;
import com.jqsoft.launcher.R;
import com.garea.medical.MedicalFactory;
import com.garea.medical.nibp.INibp;
import com.garea.medical.nibp.INibp.OnNibpListener;
import com.garea.medical.nibp.INibpData;
import com.garea.medical.nibp.INibpState;

import org.json.JSONObject;

/**
 * 测量血压
 * @作者：周桃有
 * @日期：2015-7-8
 */
public class NibpExample extends JQActivity {
	private INibp mNibp;
	
	private TextView tvState, tvDia, tvSys, tvPress;
	private Button btnControl,btnNibpSave;
	
	private byte state = INibpState.NIBP_STATE_IDEL;
	
	private String shuzy="",shousy="";
	
	@Override
	protected int getContentLayout() {
		
		return R.layout.example_nibp;
	}

	@Override
	protected void initGui() {
		btnControl = (Button) this.findViewById(R.id.btnNibpControl);
		btnNibpSave = (Button) this.findViewById(R.id.btnNibpSave);
		tvState = (TextView) this.findViewById(R.id.tvNibpState);
		tvDia = (TextView) this.findViewById(R.id.tvDia);
		tvSys = (TextView) this.findViewById(R.id.tvSys);
		tvPress = (TextView) this.findViewById(R.id.tvCurrPress);
		
	}

	@Override
	protected void initAction() {
		setGoBack();
		btnControl.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (state == INibpState.NIBP_STATE_IDEL) {
					/**
					 * 开始测量
					 */
					mNibp.start();
					btnControl.setText("停止测量");
				} else {
					/**
					 * 停止测量
					 */
					mNibp.stop();
					btnControl.setText("开始测量");
					tvDia.setText("---");
					tvSys.setText("---");
				}
			}
			
		});
		btnNibpSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				JSONObject jsonObject = new JSONObject();
				try {
					jsonObject.put("cSP", shousy);//收缩压
					jsonObject.put("cDP", shuzy);//舒张压
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//设置当前activity被关闭的时候的结果数据
				Intent data = new Intent();
				data.putExtra("jsonstr", jsonObject.toString());
				setResult(3, data);
				//关闭当前的activity
				NibpExample.this.finish();
			}
			
		});
		
		initNibp();
		
	}

	@Override
	protected void initData() {
		setTitle("测量血压");
		
	}
	
	private void initNibp() {
		mNibp = MedicalFactory.createLocalNibp();
		mNibp.setListener(new OnNibpListener() {
			@Override
			public void onReceivedData(INibpData d) {
				//接收到的血压数据
				if (d.isDiaValid()) {
					shuzy=Integer.toString(d.getDia());
					tvDia.setText(shuzy);
				}
				
				if (d.isSysValid()) {
					shousy=Integer.toString(d.getSys());
					tvSys.setText(shousy);
				}
			}

			@Override
			public void onReceivedState(INibpState s) {
				//接收到的血压状态
				if (s.getState() == INibpState.NIBP_STATE_IDEL) {
					tvState.setText("空闲");
					tvPress.setText("---");
				} else {
					tvState.setText("测量中");
					tvPress.setText(Integer.toString(s.getCurrentPress()));
				}
				
				state = s.getState();
			}
			
			@Override
			public void onInited() {
				Log.i("NibpExample", "血压模块初始化成功");
			}
		});
		mNibp.init();
	}
	
	@Override
	public void onStop() {
		super.onStop();
		mNibp.stop();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		mNibp.uninit();
	}
	
//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		getMenuInflater().inflate(R.menu.main, menu);
//		return true;
//	}
}
